package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<Person> personList;
    private Context context;

    public MyAdapter(Context context, List<Person> personList) {
        if (context == null || personList == null) {
            throw new IllegalArgumentException("Context or personList cannot be null.");
        }
        this.context = context;
        this.personList = personList;
    }

    @Override
    public int getCount() {
        return personList.size();
    }

    @Override
    public Object getItem(int position) {
        return personList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PersonHolder personHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
            personHolder = new PersonHolder();
            personHolder.imgIcon = convertView.findViewById(R.id.img_icon);
            personHolder.txName = convertView.findViewById(R.id.tx_name);
            personHolder.txXuefen = convertView.findViewById(R.id.tx_xuefen);
            personHolder.txMaxNum = convertView.findViewById(R.id.tx_maxNum);
            personHolder.txSelectedNum = convertView.findViewById(R.id.tx_selectedNum);
            convertView.setTag(personHolder);
        } else {
            personHolder = (PersonHolder) convertView.getTag();
        }

        Person person = personList.get(position);
        personHolder.imgIcon.setImageResource(person.getImgIcon());
        personHolder.txName.setText(person.getName());
        personHolder.txXuefen.setText( "学分:"+ person.getXuefen());
        personHolder.txMaxNum.setText("课程容量：" + person.getMaxNum());
        personHolder.txSelectedNum.setText("已选人数：" + person.getSelectedNum());

        // 添加按钮点击事件
        personHolder.txName.setOnClickListener(v -> {
            Intent intent = new Intent(context, activity_spring.class);
            // 传递课程信息
            intent.putExtra("courseName", person.getName());
            intent.putExtra("courseCredit", person.getXuefen());
            intent.putExtra("maxNum", person.getMaxNum());
            intent.putExtra("selectedNum", person.getSelectedNum());
            intent.putExtra("courseImage", person.getImgIcon());
            context.startActivity(intent);
        });

        return convertView;
    }

    static class PersonHolder {
        ImageView imgIcon;
        Button txName;
        TextView txXuefen;
        TextView txMaxNum;
        TextView txSelectedNum;
    }
}